#!/bin/bash

bin=http_server
port=8888
id=$(pidof $bin)
echo $id
function start()
{
    if [ -z "$id" ];then
        ./$bin $port
        echo "$id process running end ..."
    else
        echo "$bin is running!"
    fi
}
function stop()
{
    if [ -z "$id" ];then
        echo "$id process is not exists!"
    else
        kill -9 $id
        echo "$bin is end!"
        id=''
    fi

}
function status()
{
    if `pidof $bin > /dev/null`;then
        echo "status: running"
    else
        echo "status: dead"
    fi
}
case $1 in
    "start" )
        start
        ;;
    "stop" )
        stop
        ;;
    "restart" )
        stop
        start
        ;;
    "status" )
        status
        ;;
    * )
        echo "usage: $0 start | stop | status"
        ;;
esac
##shell脚本没有main函数，依次往下执行，碰到函数不执行，直到遇到调用函数才去执行函数;
##shell脚本中函数为空会报错，加上一个冒号即可解决错误
